package cn.rui.service;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.extra.mail.MailUtil;
import cn.rui.mapper.LoginLogMapper;
import cn.rui.mapper.UserMapper;
import cn.rui.pojo.LoginLog;
import cn.rui.pojo.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 异步执行方法
 * @author 徽州大都督
 * @date 2021/6/11
 */
@Service
//@Transactional
public class AsyncService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LoginLogMapper loginLogMapper;

    @Async("taskExecutors")
    public void sendMail(){
        String str = ResourceUtil.readUtf8Str("mail.txt");
        MailUtil.send("245298151@qq.com", "修改密码通知", str, true);
    }

    /**
     * 保存登录日志
     * @param accountDisabled
     * @param i
     */
    @Async("taskExecutors")
    public void saveLoginLog(String accountDisabled,String username, int status) {

        QueryWrapper<User> wrapper = new QueryWrapper<> ();
        wrapper.eq ("username",username);
        User user = userMapper.selectOne (wrapper);
        LoginLog loginLog = new LoginLog ();
        loginLog.setLoginInfo (accountDisabled);
        loginLog.setLoginStatus (status);
        loginLog.setLoginTime (new Date ());
        loginLog.setUserId (user.getId ());
        loginLogMapper.insert (loginLog);
    }
}
